from urllib import request
import re
import json

username = input("username:")
cookie = input("cookie:")


ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.50"
referer = "https://www.educoder.net/users/%s/classrooms" % username
headers = {
    "User-Agent": ua,
    "Referer": referer,
    "Cookie": cookie,
}

# username username
course_list_url = "https://data.educoder.net/api/users/%s/courses.json?username=%s"
"""
type对应关系

homework_commons.json:
1-图文作业
3-分组作业
4-课堂实验


"""
# course-id course-id course-id type-num
course_item_url = "https://data.educoder.net/api/courses/%s/homework_commons.json?coursesId=%s&id=%s&limit=114514&type=%d"

homework_types = {"图文": 1, "实训": 4, "分组": 3}

deadlines = {}


course_list = []
course_list_req = request.Request(
    url=course_list_url % (username, username), headers=headers
)
course_list_res = request.urlopen(course_list_req)
course_json = json.loads(course_list_res.read().decode("utf-8"))
for i in course_json["courses"]:
    course_list.append(
        (i["name"], re.findall(r"^/[a-z]*/([a-zA-Z0-9]*)/", i["first_category_url"])[0])
    )

for course, courseid in course_list:
    deadlines[course] = {}
    # 图文作业
    normal_homework_req = request.Request(
        url=course_item_url % (courseid, courseid, courseid, homework_types["图文"]),
        headers=headers,
    )
    normal_homework_res = request.urlopen(normal_homework_req)
    normal_homework_json = json.loads(normal_homework_res.read().decode("utf-8"))
    deadlines[course][normal_homework_json["main_category_name"]] = {
        "total_count": normal_homework_json["task_count"],
        "homeworks": [
            {
                "name": i["name"],
                "end_time": i["end_time"],
                "unfinished": i["un_commit_work"],
            }
            for i in normal_homework_json["homeworks"]
        ],
    }
    unfinished = 0
    for i in normal_homework_json["homeworks"]:
        if i["un_commit_work"]:
            unfinished += 1
    deadlines[course][normal_homework_json["main_category_name"]][
        "unfinished_count"
    ] = unfinished

    # 实训作业
    shixun_homework_req = request.Request(
        url=course_item_url % (courseid, courseid, courseid, homework_types["实训"]),
        headers=headers,
    )
    shixun_homework_res = request.urlopen(shixun_homework_req)
    shixun_homework_json = json.loads(shixun_homework_res.read().decode("utf-8"))
    deadlines[course][shixun_homework_json["main_category_name"]] = {
        "total_count": shixun_homework_json["task_count"],
        "homeworks": [
            {
                "name": i["name"],
                "end_time": i["end_time"],
                "unfinished": not bool(i["shixun_finished_status"]),
            }
            for i in shixun_homework_json["homeworks"]
        ],
        "unfinished": shixun_homework_json["task_count"]
        - shixun_homework_json["finished_task_count"],
    }

deadlines_json = json.dumps(deadlines, ensure_ascii=False)
open("tmp.json", "w", encoding="utf-8").write(deadlines_json)
